<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use HasFactory, Notifiable, SoftDeletes;
    protected $table = 'eb_users';

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';
    const DELETED_AT = 'delete_time';


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username',
        'realname',
        'name',
        'email',
        'password',
        'ip',
        'status',
        'phone',
        'company_name',
        'last_login_time',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        // 'email_verified_at' => 'datetime',
    ];

    // 用户微信 一对一
    public function wx()
    {
        return $this->hasOne(WxloginMember::class, 'user_id', 'id');
    }

    protected static function boot()
    {
        parent::boot();
        static::deleting(function ($user) {
            $user->wx()->delete();
        });
    }
    public function auth()
    {
        return $this->hasOne(UsersAuthen::class, 'user_id', 'id');
    }

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims(): array
    {
        return [];
    }

    //判断该手机号是否注册
    public static function checkPhone($phone = '')
    {
        return self::where('username', $phone)->orWhere('phone', $phone)->count();
    }


    /**
     * 当绑定手机号没有这个手机号码的时候去进行注册
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-07-06
     * @param array $payload
     * @return mixed
     */
    public static function weRegister($payload)
    { //WxloginMember
        if (self::checkPhone($payload['phone']))  return ['status' => 0, 'msg' => "当前手机号已被注册"];
        $wx_info = WxloginMember::where(['unionid' => $payload['open_id']])->whereNull('delete_time')->first();
        noticeLog('微信用户信息' . json_encode($wx_info));
        if (empty($wx_info)) {
            return fail_message('未获取微信用户信息');
        }
        $info = [
            'username' => $payload['phone'],
            'realname' => $wx_info['nickname'],
            'avatar' => $wx_info['headimgurl'],
            'password' => bcrypt(123456),
            'ip' => request()->ip(),
            'company_name' => isset($payload['company_name']) ? $payload['company_name'] : '',
            'phone' => $payload['phone'],
            'status' => 1,
        ];

        $member = self::create($info);

        if (!$member) return ['status' => 0, 'msg' => "注册失败"];
        noticeLog('用户注册成功' . json_encode($member));
        return $member;
    }
}
